package com.module.pubpage.ui.videoPlay

import android.net.Uri
import android.view.View
import androidx.annotation.NonNull
import androidx.annotation.OptIn
import androidx.media3.common.MediaItem
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.ExoPlayer
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.common.app.base.AppActivity
import com.common.app.core.RouterHub
import com.common.app.data.bean.KeyBundle
import com.common.app.utls.HeadTitleUtils
import com.common.base.app.extras.otherwise
import com.common.base.app.extras.yes
import com.common.base.utils.*
import com.module.pubpage.databinding.PubpageActivityVideoPlayBinding
import com.module.pubpage.ui.PubPageViewModel


/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：29/11/2023
 *
 * 描述：视频播放页
 *
 * 修订历史：
 *
 */
@Route(path = RouterHub.ROUTER_PUBPAGE_VIDEO_PLAY_MAIN)
class PubPageVideoPlayMainActivity : AppActivity<PubpageActivityVideoPlayBinding, PubPageViewModel>(),
    View.OnClickListener {

    @Autowired(name = KeyBundle.BUNDLE_KEY)
    @JvmField
    var mUrl: String? = ""

    @Autowired(name = KeyBundle.PATH_URL)
    @JvmField
    var localPath: String? = ""

    private val playerBuilder = ExoPlayer.Builder(this)
    private var mExoPlayer: ExoPlayer?=null

    override fun getViewModel(): Class<PubPageViewModel> = PubPageViewModel::class.java

    override fun getViewContentBinding() = PubpageActivityVideoPlayBinding.inflate(layoutInflater)
    override fun isShowToolBar(): Boolean = false
    override fun isStatusBarTranslate(): Boolean = true

    @OptIn(UnstableApi::class) override fun initViews() {
        super.initViews()
        StatusBarUtils.setStatusBarHeight(mViewContentBinding.statusView)

        val headTitleUtils = HeadTitleUtils(this)
        headTitleUtils.setTitle("视频")
        headTitleUtils.setLineStyle(4)

//        mViewContentBinding.videoCjs.controllerHideOnTouch = false// 设置播放界面触碰后是否自动隐藏，默认是true
        mViewContentBinding.videoCjs.controllerAutoShow = false// 设置播放界面是否自动显示，默认是true
        mViewContentBinding.videoCjs.controllerShowTimeoutMs = 2000// 设置播放界面自动隐藏时间，默认5000
        mViewContentBinding.videoCjs.controllerShowTimeoutMs = 2000// 设置播放界面自动隐藏时间，默认5000
//        mViewContentBinding.videoCjs.setShowRewindButton(false)// 是否显示快退按钮
//        mViewContentBinding.videoCjs.setShowFastForwardButton(false)// 是否显示快进按钮
        mViewContentBinding.videoCjs.setShowPreviousButton(false)// 是否显示上一个按钮
        mViewContentBinding.videoCjs.setShowNextButton(false)// 是否显示下一个按钮
    }

    override fun initEvents() {
        super.initEvents()

    }

    override fun lazyInitData() {
        super.lazyInitData()
        setVideoData()
    }

    override fun onPause() {
        mExoPlayer?.pause()
        super.onPause()
    }

    override fun onDestroy() {
        mExoPlayer?.stop()
        mExoPlayer?.release()
        super.onDestroy()
    }

    override fun onClick(v: View?) {
    }


    private fun setVideoData() {
        localPath.isNullOrEmpty().yes {
            mUrl?.let {
                if (mExoPlayer ==null){
                    mExoPlayer = playerBuilder.build()
                    mViewContentBinding.videoCjs.player = mExoPlayer
                }
                mExoPlayer?.removeMediaItem(0)
                mExoPlayer?.setMediaItem(MediaItem.fromUri(Uri.parse(it)))
                mExoPlayer?.prepare()
//            mExoPlayer?.play()
            }
        }.otherwise {
            // 本地视频
//            val mediaItem = MediaItem.fromUri("file:///path/to/your/video.mp4")
//            val mediaItem = localPath?.let { it1 -> MediaItem.fromUri(it1) }
        }
    }


}